from flask import Blueprint, request, url_for, session, redirect

admin_blu = Blueprint('admin', __name__, url_prefix='/admin')

from . import views


@admin_blu.before_request
def check_admin():
    # 每次请求后台管理界面时做一次验证
    if not request.url.endswith(url_for('admin.login')):
        # 如果管理员已经登录过，则直接跳转到管理页面
        user_id = session.get("user_id", None)
        is_admin = session.get("is_admin", False)
        if not user_id and is_admin:
            return redirect(url_for('admin.index'))
